package com.example.homework0112;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private int index=1;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
    }
    private void initview(){
        //1.获取listview
        RecyclerView listview=findViewById(R.id.list_view);
        TextView addlist=findViewById(R.id.list_add);
        LinearLayoutManager llm=new LinearLayoutManager(this);
        listview.setLayoutManager(llm);


        //2.绘制条目列表
        //3.创建CustomAdapter,重写里面的方法
        //4.创建ContactModel类，将每个控件的名字设为全局变量，并生成其set，get方法
        //5.在CustomAdapter类中创建private List<ContactModel>data;并生成其set方法跟构造方法；
        List<ContactModel>data=new ArrayList<>();
        ContactModel item=new ContactModel();
        item.setListicon(R.drawable.change);
        item.setListmax("5");
        item.setListtitle("嫦娥仙子");
        item.setListdate("12:20");
        item.setListtitle2("在干嘛");
        data.add(item);
        ContactModel houge=new ContactModel();
        houge.setListicon(R.drawable.houge);
        houge.setListmax("5");
        houge.setListtitle("猴哥");
        houge.setListdate("12:20");
        houge.setListtitle2("有个妖精搞不定，搭把手");
        data.add(houge);
        ContactModel sanzang=new ContactModel();
        sanzang.setListicon(R.drawable.sanzang);
        sanzang.setListmax("3");
        sanzang.setListtitle("唐僧");
        sanzang.setListdate("12:20");
        sanzang.setListtitle2("救救我，大哥");
        data.add(sanzang);
        ContactModel bajie=new ContactModel();
        bajie.setListicon(R.drawable.bajie);
        bajie.setListmax("6");
        bajie.setListtitle("八戒");
        bajie.setListdate("12:20");
        bajie.setListtitle2("八戒，你个懒虫");
        data.add(bajie);
        ContactModel gril=new ContactModel();
        gril.setListicon(R.drawable.grilwang);
        gril.setListmax("3");
        gril.setListtitle("女儿国主");
        gril.setListdate("12:20");
        gril.setListtitle2("哥哥，哥哥");
        data.add(gril);
        ContactModel guanyin=new ContactModel();
        guanyin.setListicon(R.drawable.guanyin);
        guanyin.setListmax("7");
        guanyin.setListtitle("观音");
        guanyin.setListdate("12:20");
        guanyin.setListtitle2("施主近来可好");
        data.add(guanyin);
        ContactModel jinjiao=new ContactModel();
        jinjiao.setListicon(R.drawable.jinjiao);
        jinjiao.setListmax("1");
        jinjiao.setListtitle("金角");
        jinjiao.setListdate("12:20");
        jinjiao.setListtitle2("哥，猴子打我");
        data.add(jinjiao);
        ContactModel rulai=new ContactModel();
        rulai.setListicon(R.drawable.rulai);
        rulai.setListmax("1");
        rulai.setListtitle("如来");
        rulai.setListdate("12:20");
        rulai.setListtitle2("兄弟，还好吗");
        data.add(rulai);
        ContactModel shasen=new ContactModel();
        shasen.setListicon(R.drawable.shaseng);
        shasen.setListmax("1");
        shasen.setListtitle("沙和尚");
        shasen.setListdate("12:20");
        shasen.setListtitle2("师傅被妖怪抓走了");
        data.add(shasen);
        ContactModel yinjiao=new ContactModel();
        yinjiao.setListicon(R.drawable.yinjiao);
        yinjiao.setListmax("1");
        yinjiao.setListtitle("银角");
        yinjiao.setListdate("12:20");
        yinjiao.setListtitle2("今晚去蹦迪否");
        data.add(yinjiao);
        ContactModel yudi=new ContactModel();
        yudi.setListicon(R.drawable.yudi);
        yudi.setListmax("1");
        yudi.setListtitle("玉帝");
        yudi.setListdate("12:20");
        yudi.setListtitle2("七仙女还可以吗，哥");
        data.add(yudi);
        ContactModel yaojing=new ContactModel();
        yaojing.setListicon(R.drawable.zhizhujing);
        yaojing.setListmax("1");
        yaojing.setListtitle("蜘蛛精");
        yaojing.setListdate("12:20");
        yaojing.setListtitle2("奴家仰慕官人已久");
        data.add(yaojing);
        CustomAdapter adapter=new CustomAdapter(this);
        adapter.setData(data);
        listview.setAdapter(adapter);
        adapter.setListener(new CustomAdapter.onItemViewClickListener() {
            @Override
            public void onItemLongClick(int position) {
                new AlertDialog.Builder(MainActivity.this).setMessage("您确定要删除该条联系人吗")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //移除该条目
                                data.remove(position);
                                adapter.notifyItemRemoved(position);//移除数据
                                adapter.notifyDataSetChanged();//刷新

                            }
                        }).setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //取消弹框
                        dialog.dismiss();
                    }
                }).setNeutralButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //取消弹框
                        dialog.dismiss();
                    }
                }).create().show();
            }

            @Override
            public void list_icon(int position) {
                Toast.makeText(MainActivity.this,"头像"+position+"被点击",Toast.LENGTH_LONG).show();
            }
        });

        addlist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击按钮，向列表新增一条数据
                ContactModel boy=new ContactModel();
                boy.setListicon(R.drawable.boy);
                boy.setListmax("1");
                boy.setListtitle("十亿少女的梦"+index);
                boy.setListdate("12:20");
                boy.setListtitle2("男神，男神，干嘛呢");
                data.add(0,boy);
                index++;

                adapter.notifyItemInserted(0);
                listview.smoothScrollToPosition(0);
                adapter.notifyDataSetChanged();//刷新
            }
        });
    }



}